#!/usr/bin/env bash

start() {
	status
	if [ ! $? -eq 0 ]; then
		echo "process is already running.."
		return 1
	fi

    mkdir -p logs

	pip install supervisor -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com

    # 启动后台程序
	supervisord -c spider-platform.conf

    echo "process start success..."
}

stop() {

    ps -ef | grep -v grep | grep  supervisord | grep -w spider-platform.conf | awk '{print $2}' | xargs kill -9

    ps -ef | grep -v grep | grep  python | grep -w spider_scheduler.py | awk '{print $2}' | xargs kill -9

	echo "process stop success..." && return 1
}

restart() {
    stop
    sleep 1
    start
}

status() {

    result=1
    pid=`ps -ef | grep -v grep | grep  supervisord | grep -w spider-platform.conf | awk '{print $2}'`
    if [ -z "${pid}" ]; then
        echo "后台管理服务不存在..."
        result=0
    else
        echo "supervisord ${pid}"
    fi
    return ${result}
}

case "$1" in
	start|stop|restart|status)
  		$1
		;;
	*)
		echo $"Usage: $0 {start|stop|status|restart}"
		exit 1
esac
